package com.baymax.test.scene.demo

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentContainer
import androidx.fragment.app.FragmentFactory
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.commit
import com.baymax.test.scene.BaseGroupScene
import com.baymax.test.scene.Hook
import com.baymax.test.scene.addButton
import com.baymax.test.scene.addDFView
import com.baymax.test.scene.newFlexContainer
import com.baymax.test.scene.newSceneFlexContainer
import com.bytedance.scene.Scene
import com.bytedance.scene.ktx.replace
import java.io.PrintWriter

class FragmentScene : BaseGroupScene() {
    val fg = object : FragmentFactory() {
        override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
            println("instantiate $classLoader , $className")
            return super.instantiate(classLoader, className)
        }
    }

    override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): ViewGroup {
        return p1.newSceneFlexContainer {

            val vid1 = View.generateViewId()
            addButton("add f1") {
                val spm = (requireActivity() as AppCompatActivity).supportFragmentManager
                println("spm $spm")
                spm.fragmentFactory = fg
                spm.commit {
                    replace(vid1, ChildFragment(), "t1")
                }
            }

            addButton("replace scene 1") {
                replace(vid1, ChildScene(), "s1")
            }


            addButton("test child frag") {

            }

            addDFView(300, 300, FrameLayout(context)) {
                id = vid1

            }


        }
    }

    class ChildScene : Scene() {
        override fun onCreateView(p0: LayoutInflater, p1: ViewGroup, p2: Bundle?): View {
            return View(p1.context).apply {
                setBackgroundColor(Color.GREEN)
            }
        }

    }

    class ChildFragment : Fragment() {
        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return View(container!!.context).apply {
                setBackgroundColor(Color.YELLOW)
                setOnClickListener {
                    val childFM = this@ChildFragment.childFragmentManager
                    println("child $childFM")
                    PrintWriter(System.out).use {
                        childFM.dump("child -->", null, it, null)
                    }
                    hookFM(childFM)
                    PrintWriter(System.out).use {
                        childFM.dump("hook -->", null, it, null)
                    }
                }
            }

        }

    }

    companion object {
        fun hookFM(fm: FragmentManager) {

           Hook.hookFragmentContainer(fm)

        }

    }

    class DelegateFMContainer(private val f: FragmentContainer) : FragmentContainer() {
        override fun onFindViewById(id: Int): View? {
            return f.onFindViewById(id)
        }

        override fun onHasView(): Boolean {
            return f.onHasView()
        }

        override fun instantiate(
            context: Context,
            className: String,
            arguments: Bundle?
        ): Fragment {
            return super.instantiate(context, className, arguments)
        }

        override fun toString(): String {
            return "DelegateFMContainer(f=$f)"
        }


    }
}
